import json

from django.http import HttpResponse
from django.shortcuts import redirect

from entity.common.response import ResponseStatus, ResponseData
from userApp.service.user.userservice import UserService
from utils.transfer import transferToJson

userService = UserService()
#

def loginVerify(request):
    response = None
    if request.method == 'POST':
        user = json.loads(request.body)['user']
        result = None if not user else userService.verifyUser(user)
        if result:
            request.session['user'] = transferToJson(result)
            response = ResponseData(True, ResponseStatus.SUCESS)
    if not response:
        response = ResponseData(success=False, responseCode=ResponseStatus.USERINFO_ERROR, message="用户名或密码错误")
    # 将返回值转换为json格式
    return HttpResponse(content=transferToJson(response), content_type=ResponseData.JSON_CONTENT_TYPE)

def logout(request):
    request.session.flush()
    return redirect('login.html')


